<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * 用户注册页面
	 */
	public function index()
	{
		if($this->session->userdata('user_id'))
		{
			header('location:'.base_url('ucenter'));
		}
		$data = array('title'=>'用户登录 - 作业本','h_title'=>'用户登录','style'=>'login/login');
		
		$this->load->view('header_980',$data);
		$this->load->view('login/login');
		$this->load->view('footer/simplefooter');
		
	}
	
	/**
	 * 处理用户登录
	 * @param unknown $email
	 */
	public function loginAct()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('m_user');
		$login = $this->input->post();

		try{
				
			//验证邮箱地址格式是否正确
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10001');
			}
			
			//根据邮箱地址验证用户是否存在
			$user = $this->m_user->selectUserByEmail($login['email']);
			
			if(!$user)
			{
				throw new Exception('10003');
			}
				
			//验证密码是否正确
			$this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[16]');
			if ($this->form_validation->run() == FALSE)
			{
				throw new Exception('10002');
			}
			
			//验证用户的密码是否正确
			if( sha1(md5($login['password']).$user['0']->validcode) !== $user['0']->user_password)
			{
				throw new Exception('10004');
			}

		}catch (Exception $e){
			echo $e->getMessage();
			exit;
		}

		$newdata = array('user_id'  => $user['0']->user_id,'user_nickname' => $user['0']->user_nickname);
			
		$this->session->set_userdata($newdata);
			
		echo '10000';
	}
	
	/**
	 * 用户退出
	 * @param unknown $email
	 */
	public function loginout()
	{
		$this->session->sess_destroy();
		header('location:'.base_url('login'));
	}
	
	
	
	public function sendEmail($email)
	{
		$this->load->library('email');
		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'smtp.163.com';
		$config['smtp_user'] = 'yanyinghq@163.com';
		$config['smtp_pass'] = 'gx1992x03y23hq';
		$this->email->initialize($config);
		$this->email->from('yanyinghq@163.com', 'Your Name');
		$this->email->to($email);
		$this->email->subject('Email Test');
		$this->email->message('Testing the email class.');
		$this->email->send();
	}
}

/* End of file Register.php */
/* Location: ./application/controllers/Register.php */